<?php

namespace app\index\controller;

use app\admin\model\Article;
use app\admin\model\Book;
use app\admin\model\Link;
use app\common\controller\Frontend;
use app\common\model\Category;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\response\View;

class Index extends Frontend
{
    public function _initialize()
    {
        $category = Category::where(["status" => "normal", "type" => "item"])
            ->order("weigh desc,id desc")
            ->field("id,name,url")
            ->select();
        $this->assign("category", $category);

        $this->assign("action", "/" . $this->request->action());

    }

    /**
     * 首页
     * @return View
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function index(): View
    {
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);

        //精品案例1
        $product01 = Article::where(["status" => 1, 'type' => 3, "is_home" => 2])
            ->order("weigh desc,id desc")
            ->limit(16)
            ->field("id,title,image")
            ->select();
        $this->assign("product01", $product01);
        //热门资讯
        $newArticle = Article::where(["status" => 1, 'type' => 1, "is_home" => 2])
            ->order("weigh desc,id desc")
            ->limit(10)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        //行业动态
        $industryArticle = Article::where(["status" => 1, 'type' => 2, "is_home" => 2])
            ->order("weigh desc,id desc")
            ->limit(10)
            ->field("id,title,image")
            ->select();
        $this->assign("industryArticle", $industryArticle);
        //精品案例2
        $product02 = Article::where(["status" => 1, 'type' => 3, "is_home" => 2])
            ->order("weigh desc,id desc")
            ->limit(16, 16)
            ->field("id,title,image")
            ->select();
        $this->assign("product02", $product02);
        //友情链接
        $link = Link::where(["status" => 1])
            ->order("weigh desc,id desc")
            ->field("id,name,url")
            ->select();
        $this->assign("link", $link);
        return view();
    }

    /**
     * 关于我们
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function about(): View
    {
        $find = Category::where("id", 1)->find();
        $this->assign("find", $find);
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);
        //最新咨询
        $newArticle = Article::where(["status" => 1, 'type' => 1])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        return view();
    }


    /**
     * 藏品中心
     * @param string $cate_id
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function product(string $cate_id = ""): View
    {
        $find = Category::where("id", 2)->find();
        $this->assign("find", $find);

        $category_name = $cate_id != "" ? Category::where("id", $cate_id)->value("name") : "全部";
        $this->assign("category_name", $category_name);
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);
        //最新咨询
        $newArticle = Article::where(["status" => 1, 'type' => 1])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        $product = Article::where(function ($query) use ($cate_id) {
            $query->where(["status" => 1, 'type' => 3]);
            if ($cate_id) {
                $query->where("category_id", $cate_id);
            }
        })
            ->order("weigh desc,id desc")
            ->field("id,title,image")
            ->paginate(9, false, ["query" => request()->param()]);
        $this->assign("product", $product);
        $this->assign("page", $product->render());
        $this->assign("cate_id", $cate_id);
        return view();
    }

    /**
     * 热门资讯 and 行业动态
     * @param int $t
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function news(int $t = 1): View
    {
        $find = Category::where(function ($q) use ($t) {
            if ($t == 1) {
                $q->where("id", 7);
            } else {
                $q->where("id", 3);
            }
        })->find();
        $this->assign("find", $find);
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);
        //最新咨询
        $newArticle = Article::where(["status" => 1, 'type' => 1])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        $article = Article::where(["status" => 1, 'type' => $t])
            ->order("weigh desc,id desc")
            ->field("id,title,image,createtime")
            ->paginate(15, false, ["query" => request()->param()]);
        $this->assign("article", $article);
        $this->assign("page", $article->render());
        $this->assign("category_name", $t == 1 ? '热门资讯' : '行业动态');
        $this->assign("t", $t);
        return view();
    }

    /**
     * 联系我们
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function contact()
    {
        $find = Category::where("id", 5)->find();
        $this->assign("find", $find);
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);
        //最新咨询
        $newArticle = Article::where(["status" => 1, 'type' => 1])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        return view();
    }

    /**
     * 留言
     * @return View|void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function book()
    {
        if ($this->request->isAjax()) {
            $paras = $this->request->post();
            if (!$paras["name"]) $this->error("姓名不能为空");
            if (!$paras["content"]) $this->error("内容不能为空");
            if (!$paras["mebile"] || strlen($paras['mebile']) != 11) $this->error("手机号不能为空或格式不正确");
            if (!captcha_check($paras['yzm'])) {
                // 验证失败
                $this->error("验证码错误");
            };
            Book::create([
                "name" => $paras["name"],
                "content" => $paras["content"],
                "mebile" => $paras["mebile"],
            ]);
            $this->success("提交成功");
        } else {
            $find = Category::where("id", 4)->find();
            $this->assign("find", $find);
            $productCate = Category::where(["status" => "normal", "type" => "article"])
                ->order("weigh desc,id desc")
                ->field("id,name,image")
                ->select();
            $this->assign("productCate", $productCate);
            //最新咨询
            $newArticle = Article::where(["status" => 1, 'type' => 1])
                ->order("weigh desc,id desc")
                ->limit(5)
                ->field("id,title,image")
                ->select();
            $this->assign("newArticle", $newArticle);
            return view();
        }
    }

    /**
     * 文章详情
     * @param $id
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function info($id = ""): View
    {
        $productCate = Category::where(["status" => "normal", "type" => "article"])
            ->order("weigh desc,id desc")
            ->field("id,name,image")
            ->select();
        $this->assign("productCate", $productCate);
        //最新咨询
        $newArticle = Article::where(["status" => 1, 'type' => 1])
            ->order("weigh desc,id desc")
            ->limit(5)
            ->field("id,title,image")
            ->select();
        $this->assign("newArticle", $newArticle);
        $data = Article::where(["status" => 1, 'id' => $id])
            ->with(["inCategory"])
            ->field("id,category_id,title,image,content,createtime,number")
            ->find();
        Article::where(["id" => $id])->setInc("number");
        $this->assign("data", $data);
        return view();
    }

}
